/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package easyrgb.frame;

import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Ngoc Tan
 */
public class CTableModel extends AbstractTableModel
{
    //------------------------Variables
    //default variables
    private String[] m_columnNames =
    {
    };    
    private Object[][] m_data =
    {
    };
    //more
    private int[] m_columnNonEditable =
    {
    };
    //--------------------------Methods
    @Override
    public int getColumnCount()
    {
        return m_columnNames.length;
    }

    @Override
    public int getRowCount()
    {
        return m_data.length;
    }

    @Override
    public String getColumnName(int col)
    {
        return m_columnNames[col];
    }

    @Override
    public Object getValueAt(int row, int col)
    {
        return m_data[row][col];
    }

    @Override
    /*
     * JTable uses this method to determine the default renderer/
     * editor for each cell.  If we didn't implement this method,
     * then the last column would contain text ("true"/"false"),
     * rather than a check box.
     */
    public Class getColumnClass(int c)
    {
        return getValueAt(0, c).getClass();
    }

    @Override
    /*
     * Don't need to implement this method unless your table's
     * editable.
     */
    public boolean isCellEditable(int row, int col)
    {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
        for (int i = 0; i < m_columnNonEditable.length; i++)
        {
            if (col == m_columnNonEditable[i])
            {
                return false;
            }
        }
        return true;

    }

    @Override
    /*
     * Don't need to implement this method unless your table's
     * data can change.
     */
    public void setValueAt(Object value, int row, int col)
    {
        m_data[row][col] = value;
        fireTableCellUpdated(row, col);
    }

    /**
     * set new column name
     * @param a 
     */
    public void setColumnNames(String a[])
    {
        m_columnNames = a;
    }

    /**
     * Set new data
     * @param a 
     */
    public void setData(Object a[][])
    {
        m_data = a;
    }

    /**
     * set columns which can not editable
     * @param a 
     */
    public void setColumnNonEditable(int a[])
    {
        m_columnNonEditable = a;
    }

    /**
     * Add new row
     */
    public void addRow()
    {
        int rows = m_data.length;
        int columns = m_columnNames.length;
        Object[][] tempData = new Object[rows + 1][columns];
        //array copy
        for (int i = 0; i < rows; i++)
        {
            System.arraycopy(m_data[i], 0, tempData[i], 0, columns);
        }
        //add new row
        for (int k = 0; k < columns; k++)
        {
            if (k == 0)
            {
                if (rows == 0)
                {
                    tempData[rows][k] = "" + 0;
                }
                else
                {
                    tempData[rows][k] = "" + (Integer.parseInt((String)m_data[rows - 1][0]) + 1);
                }
            }
            else
            {
                tempData[rows][k] = "";
            }
        }
        //update data        
        m_data = tempData;
    }

    /**
     * insert new row
     */
    public void insertRow(int rowInsert)
    {
        int rows = m_data.length;
        //if no row selected
        if (rowInsert == -1 || rowInsert >= rows)
        {
            addRow();
            return;
        }
        int columns = m_columnNames.length;
        Object[][] tempData = new Object[rows + 1][columns];
        //array copy
        for (int i = 0; i <= rowInsert; i++)
        {
            System.arraycopy(m_data[i], 0, tempData[i], 0, columns);
        }
        //insert new row
        for (int k = 0; k < columns; k++)
        {
            if (k == 0)
            {
                tempData[rowInsert + 1][k] = "" + (Integer.parseInt((String)m_data[rowInsert][0]) + 1);
            }
            else
            {
                tempData[rowInsert + 1][k] = "";
            }
        }
        for (int i = rowInsert + 1; i < rows; i++)
        {
            System.arraycopy(m_data[i], 0, tempData[i + 1], 0, columns);
            tempData[i+1][0] = "" + (Integer.parseInt((String)(tempData[i+1][0])) + 1);
        }
        //update data
        m_data = tempData;
    }

    /**
     * Delete a row
     */
    public void deleteRow(int rowDel)
    {
        //if no select any row
        if (rowDel == -1)
        {
            return;
        }

        int rows = m_data.length;
        //if rowDel out of range
        if (rowDel >= rows)
        {
            return;
        }

        int columns = m_columnNames.length;
        Object[][] tempData = new Object[rows - 1][columns];
        //array copy
        for (int i = 0; i < rowDel; i++)
        {
            System.arraycopy(m_data[i], 0, tempData[i], 0, columns);
        }
        for (int i = rowDel + 1; i < rows; i++)
        {
            System.arraycopy(m_data[i], 0, tempData[i - 1], 0, columns);
            tempData[i-1][0] = "" + (Integer.parseInt((String)(tempData[i-1][0])) - 1);
        }
        //update data
        m_data = tempData;
    }    
}

